package com.jinandi.app.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.jinandi.app.domain.Authorit;
import com.jinandi.app.domain.Organization;
import com.jinandi.app.service.OrganizationService;

@Controller
@RequestMapping("/admin")
public class OrganizationController extends BaseController{
	
	private OrganizationService organizationService;
	
	@Autowired
	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}
	
	/**
	 * <列表的json数据>
	 * @param request
	 * @param modelMap
	 * @author CGC 
	 * 创建日期:2013-4-26 下午2:49:19
	 */
	@RequestMapping(value = "/org/getOrg.json", method = RequestMethod.POST)
	public void getOrgJson(WebRequest request, ModelMap modelMap) {
		Map<String, String> map = getStrictParams(request.getParameterMap());
		Map<String,Object> params = new HashMap<String,Object>();
//		if(map.containsKey("auName")){
//			params.put("auName", map.get("auName"));
//		}
//		if(map.containsKey("authority")){
//			params.put("authority", map.get("authority"));
//		}
		if(map.containsKey("pid")){
			params.put("pid", map.get("pid"));
			Page<Organization> findOrganization = organizationService.findOrganizationList(params,map);
			getFormatJsonForJtable(modelMap, findOrganization);
		}else{
			getErrorJson(modelMap, "pid不能为空!");
		}
	}

	/**
	 * <组织机构树>
	 * @param webRequest
	 * @return
	 * @author CGC 
	 * 创建日期:2013-5-14 上午8:55:55
	 */
	@RequestMapping(value = "/org/getOrgChildrenByPid.json")
	@ResponseBody
	public List<Organization> getOrgChildrenByPid(WebRequest webRequest){
		Map<String ,String> map = getStrictParams(webRequest.getParameterMap());
		if(map.containsKey("id")){
			return organizationService.getOrgChildrenByPid(Long.valueOf(map.get("id")));
		}else{
			return organizationService.getOrgRoot();
		}
	}
}
